package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.data.QueryDealCustomerAnalysisRequest;
import com.travel.server.manager.dto.response.customer.data.QueryDealCustomerAnalysisResponse;
import com.travel.server.manager.dto.response.customer.data.QueryDealCustomerAnalysisResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import com.travel.server.manager.model.dto.data.CustomerDataDto;
import com.travel.server.manager.model.dto.data.DealCustomerAnalysisDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 11:01
 * @Desc:
 */
@Component
@Slf4j
public class QueryDealCustomerAnalysisCommand extends AbstractCommand<QueryDealCustomerAnalysisRequest, QueryDealCustomerAnalysisResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(QueryDealCustomerAnalysisRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryDealCustomerAnalysisResponse handle(QueryDealCustomerAnalysisRequest request) {
        checkInput(request);
        DataDateVo dateVo = OrderDataSal.getDate(request.getType(), request);
        Date lastTime = dateVo.getLastTime();
        Date startTime = dateVo.getStartTime();
        Date endTime = dateVo.getEndTime();

        List<CustomerDataDto> customerDataList = customerDal.selectCustomerAnalysis(null, endTime);
        List<DealCustomerAnalysisDto> customerOrderDtoList = customerDal.selectDealCustomerOrder(lastTime, startTime, endTime, null);
        return makeResponse(lastTime, startTime, customerDataList, customerOrderDtoList);
    }

    private QueryDealCustomerAnalysisResponse makeResponse(Date lastTime, Date startTime, List<CustomerDataDto> customerDataList,  List<DealCustomerAnalysisDto> customerOrderDtoList){
        QueryDealCustomerAnalysisResponse response = new QueryDealCustomerAnalysisResponse();
        int currentAllCustomer = 0;
        int lastAllCustomer = 0;
        int currentNewCustomer = 0;
        int lastNewCustomer = 0;
        int currentOldCustomer = 0;
        int lastOldCustomer = 0;
        int currentAllPay = 0;
        int lastAllPay = 0;
        int currentNewPay = 0;
        int lastNewPay = 0;
        int currentOldPay = 0;
        int lastOldPay = 0;
        if(!CollectionUtil.isEmpty(customerDataList)){
            for(CustomerDataDto dto : customerDataList){
                Date date = DateUtil.getDataTime(dto.getDays());
                if(!date.before(startTime)){
                    currentNewCustomer += dto.getTotal();
                }else{
                    currentOldCustomer += dto.getTotal();
                }
                currentAllCustomer += dto.getTotal();
                if(!date.before(lastTime) && date.before(startTime)){
                    lastNewCustomer += dto.getTotal();
                }
                if(date.before(lastTime)){
                    lastOldCustomer += dto.getTotal();
                }
            }
            lastAllCustomer = currentOldCustomer;
        }
        DealCustomerAnalysisDto dto = CollectionUtil.getFirst(customerOrderDtoList);
        if(dto != null){
            currentAllPay = dto.getTotalPay();
            lastAllPay = dto.getCurrentOldPay();
            currentNewPay = dto.getCurrentNewPay();
            lastNewPay = dto.getLastNewPay();
            currentOldPay = dto.getCurrentOldPay();
            lastOldPay = dto.getLastOldPay();
        }

        AnalysisItem total = makeItem(currentAllCustomer, lastAllCustomer, currentAllPay, lastAllPay, currentAllCustomer, "全部客户");
        total.setCustomerRatio("无");
        AnalysisItem newCustomer = makeItem(currentNewCustomer, lastNewCustomer, currentNewPay, lastNewPay, currentAllCustomer, "新客户");
        AnalysisItem oldCustomer = makeItem(currentOldCustomer, lastOldCustomer, currentOldPay, lastOldPay, currentAllCustomer, "老客户");
        List<AnalysisItem> analysisItemList = Arrays.asList(total, newCustomer, oldCustomer);
        response.setAnalysisList(analysisItemList);
        return response;
    }

    private AnalysisItem makeItem(Integer currentCustomer, Integer lastCustomer, Integer currentPay, Integer lastPay, Integer total, String customerType){
        AnalysisItem item = new AnalysisItem();
        item.setCustomer(currentCustomer + "");
        item.setCustomerCompare(OrderDataSal.getProportion(currentCustomer, lastCustomer));
        item.setCustomerRatio(CommonUtil.getProportion(currentCustomer, total));
        item.setCustomerType(customerType);
        item.setPay(CommonUtil.fenToYuan(currentPay).toString());
        item.setPayCompare(OrderDataSal.getProportion(currentPay, lastPay));
        item.setVisitPay("0");
        item.setVisitPayCompare("0%");
        return item;
    }
}
